package com.ruoyi.course.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.mchange.lang.LongUtils;
import com.ruoyi.common.constant.OrderConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.quartz.util.OrderCloseUtil;
import com.ruoyi.quartz.util.ScheduleUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CourseMapper;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 课程管理Service业务层处理
 * 
 * @author bigStrong
 * @date 2025-09-27
 */
@Service
public class CourseServiceImpl implements ICourseService
{
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private ISysJobService sysJobService;

    /**
     * 查询课程管理
     * 
     * @param id 课程管理主键
     * @return 课程管理
     */
    @Override
    public Course selectCourseById(Long id)
    {
        return courseMapper.selectCourseById(id);
    }

    /**
     * 查询课程管理列表
     * 
     * @param course 课程管理
     * @return 课程管理
     */
    @Override
    public List<Course> selectCourseList(Course course)
    {
        return courseMapper.selectCourseList(course);
    }

    /**
     * 新增课程管理
     * 
     * @param course 课程管理
     * @return 结果
     */
    @Override
    public int insertCourse(Course course)
    {
        course.setCreateTime(DateUtils.getNowDate());
        course.setSubject("省：" + course.getRegion().get(0) + "市："+course.getRegion().get(1));
        return courseMapper.insertCourse(course);
    }

    /**
     * 修改课程管理
     * 
     * @param course 课程管理
     * @return 结果
     */
    @Override
    public int updateCourse(Course course)
    {
        course.setUpdateTime(DateUtils.getNowDate());
        return courseMapper.updateCourse(course);
    }

    /**
     * 批量删除课程管理
     * 
     * @param ids 需要删除的课程管理主键
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(Long[] ids)
    {
        return courseMapper.deleteCourseByIds(ids);
    }

    /**
     * 删除课程管理信息
     * 
     * @param id 课程管理主键
     * @return 结果
     */
    @Override
    public int deleteCourseById(Long id)
    {
        return courseMapper.deleteCourseById(id);
    }

    @Override
    @Transactional
    public Boolean test() throws SchedulerException {
        // 保存订单到数据库

        int delay = 10;

        String orderId = "123456789";

        SysJob orderJob = OrderCloseUtil.getOrderJob(orderId);

        boolean b = sysJobService.runCustom(orderJob, delay);

        if (b) {
            System.out.println("任务执行成功");
            return true;
        }
        System.out.println("任务执行失败");
        return false;


    }

    @Override
    public Boolean test2() {

        String orderId = "123456789";
        Long jobId = (long) Integer.parseInt(orderId);
        JobKey jobKey = ScheduleUtils.getJobKey(jobId, OrderConstants.ORDER_CLOSE_GROUP);
        TriggerKey triggerKey = TriggerKey.triggerKey(OrderConstants.ORDER_ID + jobId, OrderConstants.ORDER_CLOSE_GROUP);

        System.out.println("要删除的TriggerKey：" + triggerKey);

        return sysJobService.delCustom(jobKey, triggerKey);

    }

    /**
     * 获取定时任务剩余时间
     * @return
     */
    @Override
    public String getRemainingTime() {
        String id = "123456789";
        int number = Integer.parseInt(id);
        Long jobId = (long) number;

        JobKey jobKey = ScheduleUtils.getJobKey(jobId, OrderConstants.ORDER_CLOSE_GROUP);

        TriggerKey triggerKey = TriggerKey.triggerKey(OrderConstants.ORDER_ID + jobId, OrderConstants.ORDER_CLOSE_GROUP);

        System.out.println("要找的triggerKey:" + triggerKey);

        return sysJobService.getCustomRemainingTime(jobKey, triggerKey);
    }

    private void sout(String number) {
        System.out.println("任务开始执行"+  number);
    }

    public static void main(String[] args) {


        String time = DateUtils.getTime();
        System.out.println(time);

        Date date = DateUtils.parseDate(time);
        System.out.println(date);

        // 把time加60分钟
        Date time1 = DateUtils.addMinutes(date, 60);
        System.out.println(time1);

        // 把time1改为yyyy-MM-dd HH:mm:ss
        String s = DateUtils.dateTimeToStr(time1);
        System.out.println(s);


    }
}
